<template>
  <div>
    <el-dialog :title="formData.id ?'编辑经纪':'新增经纪'" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">

        <el-form-item label="经纪" prop="agent">
          <el-select v-model="formData.agent" placeholder="请选择">
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-select v-model="formData.category" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-tag
            v-for="tag in formData.tags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >

            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>

        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="formData.price" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" type="textarea" resize="none" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <Uploadimg ref="img1" />
          <el-input v-model="formData.image" placeholder="强制设置图片跳过上传" />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-row type="flex" justify="end">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确认</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addProperties, editProperties, editPropertiesId } from '@/api/properties'
import { getAgents } from '@/api/agents'
import { getCategories } from '@/api/categories'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        desc: '',
        image: '',
        address: '',
        price: '',
        agent: {},
        category: {},
        tags: []

      },

      rules: {
        title: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }

        ]

      },
      option: [],
      options: [],
      value: '',
      inputVisible: false,
      inputValue: ''
    }
  },
  async created() {
    // 获取经纪，分类列表
    const res = await getAgents({
      _limit: -1
    })
    const res1 = await getCategories({
      _limit: -1

    })
    this.option = res.data
    this.options = res1.data
    // console.log(1111111, this.option)
  },
  methods: {
    // 获取指定分类
    async editPropertiesId(id) {
      const res = await editPropertiesId(id)
      console.log(56, this.option, res)
      this.formData = res.data
      this.$refs.img1.imageUrl = res.data.image
      console.log(11)
    },
    closeFn() {
      this.$emit('closeEvent')
      //   使用语法：
      // 子组件：this.$emit('update:要修改的props属性名',更新的值)
      // 父组件
      // <AddRole 属性名.sync = '父组件中的变量'></AddRole>
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        title: '',
        desc: '',
        image: '',
        address: '',
        price: '',
        agent: '',
        category: '',
        tags: []

      }
      this.$refs.img1.imageUrl = ''
      // 清空效验
      this.$refs.form.resetFields()
    },
    submit() {
    //   await addProperties(this.formData)

      this.$refs.form.validate(async bool => {
        if (bool) {
          // 表单验证
          if (this.formData.id) {
            await editProperties(this.formData)
          } else {
            // 效验通过，调用接口
            if (this.$refs.img1.percent > 0 && this.$refs.img1.percent < 100) {
              return this.$message.warning('正在上传中，请稍后保存')
            }
            this.formData.image = this.$refs.img1.imageUrl || null
            await addProperties(this.formData)

            // console.log(123)
          }

          this.closeFn()
          //   更新列表
          this.$emit('updateEvent')
        }
      })
    },
    // 标签按钮
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }

}
</script>
<style lang="scss" scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
